﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Wuqi.Webdiyer;
using PM.Code.DAL;

namespace PM.Code.Web
{
    /// <summary>
    /// controlbind 的摘要说明
    /// </summary>
    public class controlbind
    {
        /// <summary>
        /// 使用公共存储过程对GridView进行分页 
        /// </summary>
        /// <param name="gv">GridView</param>
        /// <param name="pager">AspNetPager</param>
        /// <param name="tablename">数据库表名</param>
        /// <param name="primarykey">数据库主键名</param>
        /// <param name="fileds">查询字段</param>
        /// <param name="orders">排列字符（不加order by ）</param>
        /// <param name="where">查询字符(不加 where)</param>
        /// <param name="GetCount">是否取总数</param>
        public static void BindGridViewPublicProc(GridView gv,AspNetPager pager,string tablename,string primarykey,string fields,string orders,string where,bool GetCount)
        {
            if(orders!="")
                orders = " order by " + orders;
            if (tablename != "" && fields != "" && primarykey != "")
            {
                SqlParameter[] parameters ={new SqlParameter("@TableName", SqlDbType.NVarChar, 100),
                                                            new SqlParameter("@Fields", SqlDbType.NVarChar, 1000),
                                                            new SqlParameter("@Primarykey", SqlDbType.NVarChar, 1000),
                                                            new SqlParameter("@PageSize", SqlDbType.SmallInt),
                                                            new SqlParameter("@PageIndex", SqlDbType.Int),
                                                            new SqlParameter("@GetCount", SqlDbType.Bit),
                                                            new SqlParameter("@Orderby", SqlDbType.NVarChar, 200),
                                                            new SqlParameter("@strWhere", SqlDbType.NVarChar, 1000),
                                                            new SqlParameter("@TotalRow", SqlDbType.Int)};
                parameters[0].Value = tablename;
                parameters[1].Value = fields;
                parameters[2].Value = primarykey;
                parameters[3].Value = pager.PageSize;
                parameters[4].Value = pager.CurrentPageIndex;
                parameters[5].Value = GetCount ? 1 : 0;
                parameters[6].Value = orders;
                parameters[7].Value = where;
                parameters[8].Value = 0;

                parameters[8].Direction = ParameterDirection.Output;
                SqlDataReader reader = null;
                try
                {
                    
                    reader = PM.Code.DAL.DbHelper.RunProcedure("Pdmis_SplitPager", parameters);
                    gv.DataSource = reader;
                    gv.DataBind();
                }
                catch
                {
                    throw;
                }
                finally
                {
                    if (reader != null)
                    {
                        reader.Close();
                    }
                }
                if(GetCount)
                    pager.RecordCount = (int)parameters[8].Value;
            }
            else
                return;
        }

        /// <summary>
        /// 绑定DropDownList控件,如果发生异常，返回异常信息 
        /// </summary>
        /// <param name="ddl">DropDownList控件</param>
        /// <param name="strSql">SQL</param>
        /// <param name="TextField">绑定的字段（文本）</param>
        /// <param name="ValueField">绑定的字段（值）</param>
        public static void BindDropDownList(DropDownList ddl,string StrSql,string TextField, string ValueField)
        {
            SqlDataReader reader = null;
            try
            {
                reader = PM.Code.DAL.DbHelper.ExecuteDataReaderBySql(StrSql);
                while (reader.Read())
                {
                    ddl.Items.Add(new ListItem(reader[TextField].ToString(), reader[ValueField].ToString()));//增加Item
                }
            }
            catch (Exception exception)
            {
                throw new Exception(exception.Message);
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }
        }
        /// <summary>
        /// 同时绑定绑定两个DropDownList控件（一条sql语句，两个控件同数据）,如果发生异常，返回异常信息 
        /// </summary>
        /// <param name="ddl2">DropDownList控件1</param>
        /// <param name="ddl3">DropDownList控件2</param>
        /// <param name="StrSql">SQL</param>
        /// <param name="TextField">绑定的字段（文本）</param>
        /// <param name="ValueField">绑定的字段（值）</param>
        public static void BindDropDownList(DropDownList ddl1,DropDownList ddl2, string StrSql, string TextField, string ValueField)
        {
            SqlDataReader reader = null;
            try
            {
                reader = PM.Code.DAL.DbHelper.ExecuteDataReaderBySql(StrSql);
                while (reader.Read())
                {
                    ddl1.Items.Add(new ListItem(reader[TextField].ToString().Trim(), reader[ValueField].ToString().Trim()));//增加Item
                    ddl2.Items.Add(new ListItem(reader[TextField].ToString().Trim(), reader[ValueField].ToString().Trim()));//增加Item
                }
            }
            catch (Exception exception)
            {
                throw new Exception(exception.Message);
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }
        }
        /// <summary>
        /// 绑定DropDownList控件（两条sql语句，两个控件不同数据）,如果发生异常，返回异常信息
        /// </summary>
        /// <param name="ddl1">DropDownList控件1</param>
        /// <param name="ddl2">DropDownList控件2</param>
        /// <param name="strSql1">SQL1</param>
        /// <param name="strSql2">SQL2</param>
        /// <param name="TextField1">绑定的字段（文本）1</param>
        /// <param name="ValueField1">绑定的字段（值）1</param>
        /// <param name="TextField2">绑定的字段（文本）2</param>
        /// <param name="ValueField2">绑定的字段（值）2</param>
        public static void BindDropDownList(DropDownList ddl1,DropDownList ddl2, string strSql1, string strSql2, string TextField1, string ValueField1, string TextField2, string ValueField2)
        {
            SqlDataReader reader = null;
            try
            {
                string sqlstr = strSql1 + "; " + strSql2;
                reader = PM.Code.DAL.DbHelper.ExecuteDataReaderBySql(sqlstr);
                while (reader.Read())
                {
                    ddl1.Items.Add(new ListItem(reader[TextField1].ToString(), reader[ValueField1].ToString()));//增加Item
                }
                reader.NextResult();
                while (reader.Read())
                {
                    ddl2.Items.Add(new ListItem(reader[TextField2].ToString(), reader[ValueField2].ToString()));//增加Item
                }
            }
            catch (Exception exception)
            {
                throw new Exception(exception.Message);
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }
        }
        /// <summary>
        /// 绑定DropDownList控件,如果发生异常，返回异常信息
        /// </summary>
        /// <param name="ddl">DropDownList控件</param>
        /// <param name="strSql">SQL</param>
        /// <param name="Parameters">SQL参数</param>
        /// <param name="TextField">绑定的字段（文本）</param>
        /// <param name="ValueField">绑定的字段（值）</param>
        public static void BindDropDownList(DropDownList ddl, string strSql, SqlParameter[] Parameters, string TextField, string ValueField)
        {
            SqlDataReader reader = null;
            try
            {
                reader = PM.Code.DAL.DbHelper.ExecuteDataReaderBySql(strSql, Parameters);
                while (reader.Read())
                {
                    ddl.Items.Add(new ListItem(reader[ValueField].ToString(), reader[TextField].ToString()));//增加Item
                }
            }
            catch (Exception exception)
            {
                throw new Exception(exception.Message);
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }
        }
    }
}
